package com.xbdun.web.bmp.controller.system;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.xbdun.common.api.XbdUnStringResult;
import com.xbdun.common.exceptions.BizException;
import com.xbdun.common.validate.ValidateUtil;
import com.xbdun.comweb.bmp.frame.annotation.XbdunLogin;
import com.xbdun.comweb.bmp.session.CurrentUserInfo;
import com.xbdun.comweb.bmp.util.UserInfoUtil;
import com.xbdun.comweb.bmp.util.WebCommonUtil;
import com.xbdun.facade.bmp.constant.DubboBmpConst;
import com.xbdun.facade.bmp.enumx.SysOrgEnum.SysOrgTypeEnum;
import com.xbdun.facade.bmp.request.system.SysOrgRequest;
import com.xbdun.facade.bmp.result.system.LoginOrgUserMenuResult;
import com.xbdun.facade.bmp.service.system.OpenAuthFacade;
import com.xbdun.facade.bmp.service.system.SysOrgFacade;
import com.xbdun.facade.validatebmp.response.WxUnionCallPcResponse;
import com.xbdun.web.bmp.request.RegOrgInfoRequest;
import com.xbdun.web.bmp.util.ErrorPageUtil;
import com.xbdun.web.bmp.util.LoginSkipUtil;

/**
* @ClassName: IndexController
* @Description: 初始化页面
* @author zhanggt 360730440@qq.com
* @date 2017年4月14日 上午11:42:21
 */
@Controller
public class S000000Controller extends BaseSystemController{
	@Reference(registry=DubboBmpConst.REGISTRYS)
	SysOrgFacade sysOrgFacade;
	
	//机构用户信息
	@Reference(registry=DubboBmpConst.REGISTRYS)
	OpenAuthFacade openAuthFacade;
	
	//登录成功后后台管理画面
	@RequestMapping("000000")
	@XbdunLogin
	public ModelAndView mangerIndex(){
		ModelAndView view  = new ModelAndView(ROOT_PATH+"000000");
		try{
			CurrentUserInfo userInfo = UserInfoUtil.getCurrentUserInfoExcption(getRequest());
			//用户基本信息
			view.addObject("orgName",userInfo.getOrgName());//机构名称
			view.addObject("userName",userInfo.getUserName());//真实姓名
			view.addObject("jonNum",userInfo.getJonNum());//员工工号
			//当前用户的菜单
			LoginOrgUserMenuResult loginOrgUserMenu = openAuthFacade.loginOrgUserMenu(UserInfoUtil.getClient(getRequest()));
			//获取返回值
			view.addObject("code",loginOrgUserMenu.getCode());
			view.addObject("msg",loginOrgUserMenu.getMsg());
			view.addObject("success", loginOrgUserMenu.getSuccess());
			view.addObject("menus","[]");
			if(loginOrgUserMenu.getSuccess()&&CollectionUtils.isNotEmpty(loginOrgUserMenu.getData())){
				//当前用户的所有系统菜单项
				view.addObject("menus",JSON.toJSONString(loginOrgUserMenu.getData()));
			}
		}catch(Exception e){
			//异常情况同样能够登录但给出错误提示
			view.addObject("code",BizException.SYS_BUSY_0);
			view.addObject("msg","用户登录成功，但获取用户的系统菜单失败，请刷新再试");
			view.addObject("success",false);
		}
		return view;
	}
	
	//注册数据加载画面
	@RequestMapping("100000")
	public ModelAndView index(@RequestParam("data")String data,HttpServletRequest currentRequest){
		ModelAndView view  = new ModelAndView();
		try{
			LoginSkipUtil.chekedLoginQr(WebCommonUtil.getIpAddr(currentRequest),data);
			view.setViewName(ROOT_PATH+"100000");
			view.addObject("data", data);//主要用于数据签名
		}catch(Exception e){
			ErrorPageUtil.toErrorPage(view, e);
		}
		return view;
	}
	
	//新在企业进行注册
	@RequestMapping("1000001")
	@ResponseBody
	public XbdUnStringResult regOrgInfo(RegOrgInfoRequest request,HttpServletRequest currentRequest){
		XbdUnStringResult result = new XbdUnStringResult();
		try{
			addSysOrgChecked(request);
			//校验前端签名数据是否合法
			WxUnionCallPcResponse chekedLoginQr = LoginSkipUtil.chekedLoginQr(WebCommonUtil.getIpAddr(currentRequest),request.getData());
			if(StringUtils.isBlank(chekedLoginQr.getWxUnionId())){
				throw BizException.PARAM_CHKE_FAIL.newInstance("微信小程序UnionId不能为空;");
			}
			
			//企业注册时候的微信参数
			request.setXcxOpenId(chekedLoginQr.getWxId());
			request.setUnionid(chekedLoginQr.getWxUnionId());
			request.setIpAddr(chekedLoginQr.getIpAddr());
			request.setLatitude(chekedLoginQr.getLatitude());
			request.setLongitude(chekedLoginQr.getLongitude());
//			request.setAddr("需要从百度地图获取地址");
			//新增机构、机构用户、平台用户一起创建
			result = sysOrgFacade.addSysOrg(request,null);
		}catch(Exception ex){
			result.setBizException(ex);
		}
		return result;
	}
	// 检测新增企业信息
		private void addSysOrgChecked(SysOrgRequest request)throws BizException {
			if (null == request){
				throw BizException.PARAM_CHKE_FAIL.newInstance("请求对象不能为空");
			}
			StringBuilder sb = new StringBuilder();
			if (StringUtils.isBlank(request.getOrgName())) {
				sb.append("企业名不能为空;");
			}
			if (StringUtils.isBlank(request.getBusiPerson())) {
				sb.append("企业法人不能为空;");
			}
			if (StringUtils.isBlank(request.getAddr())) {
				sb.append("企业地址不能为空;");
			}
			// 判断座机的电话
			if (StringUtils.isBlank(request.getOrgMobile()) && StringUtils.isBlank(request.getOrgLandline())) {
				sb.append("企业座机和电话不能同时为空;");
			} else {
				if (null != request.getOrgMobile()) {
					if (!ValidateUtil.isMobile(request.getOrgMobile())) {
						sb.append("企业电话格式不正确;");
					}
				}
				if (null != request.getOrgLandline()) {
					if (!ValidateUtil.isPhone(request.getOrgLandline())) {
						sb.append("企业座机座机格式;");
					}
				}
			}
			if (!ValidateUtil.isEmail(request.getOrgEmail())) {
				sb.append("企业邮箱格式不正确;");
			}
			if (StringUtils.isBlank(request.getRecordNo())) {
				sb.append("企业备案号不能为空;");
			}
			if (StringUtils.isBlank(request.getRecordImg())) {
				sb.append("企业备案照片不能为空;");
			}
			if(!ValidateUtil.isIdCard(request.getIdCard())){
				sb.append("身份证号格式不正确;");
			}
			if(StringUtils.isBlank(request.getPwd())){
				sb.append("密码不能为空;");
			}
			if(null==SysOrgTypeEnum.getEnum(request.getType())){
				sb.append("企业类型未定义;");
			}
			if (sb.length() > 0) {
				throw BizException.PARAM_CHKE_FAIL.newInstance("%s", sb.toString());
			}
		}
}
